/*
Copyright 2021 LuJun

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished 
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { bufferToString } from '../core/utils/js';

/**
 * @zh
 * 缓冲读取对象
 */
export class BufferReader {
    /**
     * @zh
     * 缓冲对象
     */
    protected _buffer: ArrayBuffer = null!;

    /**
     * @zh
     * 视图
     */
    protected _view: DataView = null!;

    /**
     * @zh
     * 当前索引
     */
    protected _index: number = 0;

    /**
     * @zh
     * 缓冲容量
     */
    protected _capacity: number = 0;

    /**
     * @zh
     * 构造缓冲读取类
     *
     * @param buffer - 缓冲实体
     * @param index - 当前索引
     * @param capacity - 当前容量
     */
    constructor(buffer: ArrayBuffer, index: number = 0, capacity?: number) {
        this._buffer = buffer;
        this._view = new DataView(buffer);
        this._index = index;
        this._capacity = capacity === null ? buffer.byteLength : capacity;
    }

    /**
     * @zh
     * 跳过长度字节
     *
     * @param count - 字节数
     */
    skip(count: number) {
        this._index += count;
        if (this._index > this._capacity) {
            this._index = this._capacity;
        }
    }

    /**
     * @zh
     * 缓冲区对齐
     *
     * @param size - 对齐大小
     */
    alignment(size: number) {
        let rem = this._index % size;
        if (rem !== 0) {
            this.skip(size - rem);
        }
    }

    /**
     * @zh
     * 读取 Byte
     */
    readByte() {
        let value = this._view.getInt8(this._index);
        this.skip(1);
        return value;
    }

    /**
     * @zh
     * 读取 unsigned Byte
     */
    readUByte() {
        let value = this._view.getUint8(this._index);
        this.skip(1);
        return value;
    }

    /**
     * @zh
     * 查看 Byte
     */
    peekByte() {
        return this._view.getInt8(this._index);
    }

    /**
     * @zh
     * 查看 unsigned Byte
     */
    peekUByte() {
        return this._view.getUint8(this._index);
    }

    /**
     * @zh
     * 读取 Short
     */
    readShort() {
        let value = this._view.getInt16(this._index, true);
        this.skip(2);
        return value;
    }

    /**
     * @zh
     * 读取 unsigned Short
     */
    readUShort() {
        let value = this._view.getUint16(this._index, true);
        this.skip(2);
        return value;
    }

    /**
     * @zh
     * 查看 Short
     */
    peekShort() {
        return this._view.getInt16(this._index, true);
    }

    /**
     * @zh
     * 查看 unsigned Short
     */
    peekUShort() {
        return this._view.getUint16(this._index, true);
    }

    /**
     * @zh
     * 读取 Long
     */
    readLong() {
        let value = this._view.getInt32(this._index, true);
        this.skip(4);
        return value;
    }

    /**
     * @zh
     * 读取 unsigned Long
     */
    readULong() {
        let value = this._view.getUint32(this._index, true);
        this.skip(4);
        return value;
    }

    /**
     * @zh
     * 查看 Long
     */
    peekLong() {
        return this._view.getInt32(this._index, true);
    }

    /**
     * @zh
     * 查看 unsigned Long
     */
    peekULong() {
        return this._view.getUint32(this._index, true);
    }

    /**
     * @zh
     * 读取 Float
     */
    readFloat() {
        let value = this._view.getFloat32(this._index, true);
        this.skip(4);
        return value;
    }

    /**
     * @zh
     * 查看 Float
     */
    peekFloat() {
        return this._view.getFloat32(this._index, true);
    }

    /**
     * @zh
     * 读取 Double
     */
    readDouble() {
        let value = this._view.getFloat64(this._index, true);
        this.skip(8);
        return value;
    }

    /**
     * @zh
     * 查看 Double
     */
    peekDouble() {
        return this._view.getFloat64(this._index, true);
    }

    /**
     * @zh
     * 读取字符串，使用utf16le
     */
    readString() {
        let len = this.readUShort();
        let str = bufferToString(this.readData(len)!);
        return str;
    }

    /**
     * @zh
     * 缓冲区索引
     */
    get index() {
        return this._index;
    }

    /**
     * @zh
     * 缓冲区剩余大小
     */
    get remainSize() {
        return this._capacity - this._index;
    }

    /**
     * @zh
     * 空闲缓冲大小
     */
    get emptySize() {
        return this._buffer.byteLength - this._capacity;
    }

    /**
     * @zh
     * 读取数据
     *
     * @param count - 大小
     */
    readData(count: number) {
        if (count > 0) {
            let buffer = this._buffer.slice(this._index, this._index + count);
            this.skip(count);
            return buffer;
        }
    }

    /**
     * @zh
     * 查看数据
     *
     * @param count - 大小
     */
    peekData(count: number) {
        if (count > 0) {
            return this._buffer.slice(this._index, this._index + count);
        }
    }
}
